/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package seminario.util;

import java.io.Serializable;
import java.util.Map;
import javax.faces.application.FacesMessage;
import javax.faces.application.FacesMessage.Severity;
import javax.faces.application.NavigationHandler;
import javax.faces.context.FacesContext;
import seminario.entidade.Usuario;

/**
 *
 * @author bpmlab
 */
public class BeanUtil implements Serializable {

    public static void mensagem(Severity severity, String mensagem) {
        FacesMessage fm = new FacesMessage(severity, mensagem, null);
        FacesContext.getCurrentInstance().addMessage(null, fm);
    }

    public static void colocarNaSessao(String key, Object obj) {
        FacesContext context = FacesContext.getCurrentInstance();
        Map<String, Object> mapa = context.getExternalContext().getSessionMap();
        mapa.put(key, obj);
    }

    public static Object lerDaSessao(String key) {
        FacesContext context = FacesContext.getCurrentInstance();
        Map<String, Object> mapa = context.getExternalContext().getSessionMap();
        return mapa.get(key);
    }

    public static void removerDaSessao(String key) {
        FacesContext context = FacesContext.getCurrentInstance();
        Map<String, Object> mapa = context.getExternalContext().getSessionMap();
        mapa.remove(key);
    }

    public static boolean isSecretaria() {
        Object obj = lerDaSessao("usuarioLogado");
        if (obj == null) {
            return false;
        } else {
            Usuario usuario = (Usuario) obj;
            return usuario.getPerfil() == 'S';
        }
    }
    
    public static boolean isParticipante() {
        Object obj = lerDaSessao("usuarioLogado");
        if (obj == null) {
            return false;
        } else {
            Usuario usuario = (Usuario) obj;
            return usuario.getPerfil() == 'P';
        }
    }
    
    public static void navegar(FacesContext context, String outcome) {
        NavigationHandler navigation = context.getApplication()
                .getNavigationHandler();
        navigation.handleNavigation(context, null, outcome);
    }
}
